package com.chz.SupermarketMS.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;


import javax.sql.DataSource;

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    DataSource dataSource;

    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(dataSource)
                .usersByUsernameQuery("SELECT username, password, enabled " + " FROM user_accounts WHERE username = ?")
                .authoritiesByUsernameQuery("SELECT username, role " + "FROM user_accounts WHERE username = ?")
                .passwordEncoder(bCryptPasswordEncoder);
//        auth.inMemoryAuthentication()
//                .withUser("myser")
//                .password("pass")
//                .roles("USER");

    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()

//             .antMatchers("/commodities/new").hasRole("USER")
//               .antMatchers("/commodities/save").hasRole("ADMIN")
//
//                .antMatchers("/inventories/new").hasRole("USER")
//                .antMatchers("/inventories/save").hasRole("ADMIN")
//
//                .antMatchers("/kinds/new").hasRole("USER")
//                .antMatchers("/kinds/save").hasRole("ADMIN")
//
//                .antMatchers("/pDetails/new").hasRole("USER")
//                .antMatchers("/pDetails/save").hasRole("ADMIN")
//
//                .antMatchers("/userAccount").hasRole("USER")
//                .antMatchers("/home").hasRole("ADMIN")
//
//                .antMatchers("/heads/new").hasRole("USER")
//                .antMatchers("/heads/save").hasRole("ADMIN")
//
//                .antMatchers("/purchases/new").hasRole("USER")
//                .antMatchers("/purchases/save").hasRole("ADMIN")
//
//                .antMatchers("/sales/new").hasRole("USER")
//                .antMatchers("/sales/save").hasRole("ADMIN")
//
//                .antMatchers("/sDetails/new").hasRole("USER")
//                .antMatchers("/sDetails/save").hasRole("ADMIN")
////
                .antMatchers("/").hasRole("USER")

                .antMatchers("/**","/*").permitAll()


                .and()
                .formLogin()
                .loginPage("/login")
                .loginProcessingUrl("/login/form")
                .and()
                .logout().logoutUrl("/logout") /*自定义登出链接*/
//               .logout("/logout/form")
                .logoutSuccessUrl("/login") /*如果成功登出，需要跳转的页面*/
        ;
    }
}
